package com.easycarebn.main.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.controller.ServerController;
import com.easycarebn.request.data.AppointmentItem;
import com.easycarebn.request.data.DataItemResponse;
import com.easycarebn.request.data.StatusCode;
import com.easycarebn.request.data.UserResponse;
import com.easycarebn.service.request.OnRequest;
import com.timbacsi.main.R;

public class FragmentAppointmentDetailt extends Fragment {

	private View mView;
	private static FragmentAppointmentDetailt fragment = null;
	private static String mTitle;

	private TextView lblNgayHen;
	private TextView lblThoigian;
	private TextView lblMacuochen;
	private TextView lblDiadiem;
	private TextView lblLydokham;
	private TextView lblBaohiem;

	private TextView lblHoten;
	private TextView lblGioitinh;
	private TextView lblPhonBN;
	private TextView lblEmail;

	private TextView lblBacsi;
	private TextView lblBangcap;
	private TextView lblChuyenmon;
	private TextView lblSodienthoai;

	private TextView lblNgaytao;
	private TextView lblTrangthai;
	private TextView lblChuthichBasi;
	private TextView lblChithichBn;
	
	private LinearLayout lineBNChuthich;
	private LinearLayout lineBSChuthich;
	
	

	private EditText edChuthich;
	private Button btnCapnhat;
	private ProgressBar loadding;

	private String mId;

	public static FragmentAppointmentDetailt newInstance(String title,
			Bundle data) {
		fragment = new FragmentAppointmentDetailt();
		mTitle = title;
		fragment.setArguments(data);
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mId = getArguments().getString(Utils.KEY_ID);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.layout_detailt_booking, null);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Utils.setTitlePage(mView, mTitle);
		loadding = (ProgressBar) mView.findViewById(R.id.loadding);
		loadding.setVisibility(View.GONE);

		lineBNChuthich = (LinearLayout) mView.findViewById(R.id.lineBnChuthich);
		lineBSChuthich= (LinearLayout) mView.findViewById(R.id.lineBsChuthich);
		
		lblNgayHen = (TextView) mView.findViewById(R.id.lblNgayhen);
		lblThoigian = (TextView) mView.findViewById(R.id.lblThoigian);
		lblMacuochen = (TextView) mView.findViewById(R.id.lblMacuochen);
		lblDiadiem = (TextView) mView.findViewById(R.id.lblDiadiem);
		lblLydokham = (TextView) mView.findViewById(R.id.lbllydokham);
		lblBaohiem = (TextView) mView.findViewById(R.id.lblbaohiem);

		lblHoten = (TextView) mView.findViewById(R.id.lblHoten);
		lblGioitinh = (TextView) mView.findViewById(R.id.lblGoitinh);
		lblPhonBN = (TextView) mView.findViewById(R.id.lblDienthoai);
		lblEmail = (TextView) mView.findViewById(R.id.lblEmail);

		lblBacsi = (TextView) mView.findViewById(R.id.lblBacsi);
		lblBangcap = (TextView) mView.findViewById(R.id.lblBangCap);
		lblChuyenmon = (TextView) mView.findViewById(R.id.lblChuyenmon);
		lblSodienthoai = (TextView) mView.findViewById(R.id.lblSodienthoai);

		lblNgaytao = (TextView) mView.findViewById(R.id.lblCreadbookDate);
		lblTrangthai = (TextView) mView.findViewById(R.id.lblTrangthai);
		lblChuthichBasi = (TextView) mView.findViewById(R.id.lblChithichbs);
		lblChithichBn = (TextView) mView.findViewById(R.id.lblChithichBn);

		edChuthich = (EditText) mView.findViewById(R.id.edChuthich);
		btnCapnhat = (Button) mView.findViewById(R.id.btnCapnhat);
		btnCapnhat.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String mNote = edChuthich.getText().toString().trim();
				if (mNote.isEmpty()) {
					Utils.showMessage(
							getActivity(),
							getResources().getString(
									R.string.vui_long_nhap_chu_thich));
					return;
				}
				actionNote(mNote);

			}
		});

		getAppointmentDetailt();

	}

	public void actionNote(final String mNote) {
		UserResponse userResponse = Utils.getUserLogin(getActivity());
		if (userResponse == null) {
			Utils.showMessage(getActivity(),
					getResources().getString(R.string.vui_long_dang_nhap));
			return;
		}

		loadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);

		ServerController.getInstance(getActivity()).requestAddNoteAppointment(
				userResponse.getToken(), Integer.parseInt(mId), mNote,
				new OnRequest<DataItemResponse>() {

					@Override
					public void onSuccess(DataItemResponse data) {
						loadding.setVisibility(View.GONE);
						Utils.disableEnableControls(true, mView);
						if (data == null)
							return;
						lineBNChuthich.setVisibility(View.VISIBLE);
						lblChithichBn.setText(mNote); 
						if (data.getCode() == StatusCode.OK)
							Utils.showMessage(getActivity(), getResources()
									.getString(R.string.cap_nhat_thanh_cong));
					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.GONE);
						Utils.disableEnableControls(true, mView);
					}
				});
	}

	public void getAppointmentDetailt() {
		UserResponse uResponse = Utils.getUserLogin(getActivity());
		if (uResponse == null) {
			Utils.showMessage(getActivity(),
					getResources()
							.getString(R.string.dang_nhap_de_xem_cuoc_hen));
			mView.setVisibility(View.INVISIBLE);
			return;
		}

		loadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);

		ServerController.getInstance(getActivity()).requestGetAppointment(
				uResponse.getToken(), Integer.parseInt(mId),
				new OnRequest<AppointmentItem>() {

					@Override
					public void onSuccess(AppointmentItem data) {
						loadding.setVisibility(View.GONE);
						Utils.disableEnableControls(true, mView);
						if (data == null)
							return;
						String datatime[] = data.getTime().split(" ");
						lblNgayHen.setText(datatime[0]);
						lblThoigian.setText(datatime[1]);
						lblMacuochen.setText(data.getCode());
						if (data.getAddress().length() == 0) {
							lblDiadiem.setText(Html.fromHtml(Utils
									.setTextcolorHtml(
											getResources().getString(
													R.string.dang_cap_nhat),
											"#c4c4c4")));
						} else {
							lblDiadiem.setText(data.getAddress());
						}

						lblLydokham.setText(data.getVisitReason());
						if (data.getInsurance() == 0) {
							lblBaohiem.setText("Không");
						} else {
							lblBaohiem.setText("Có");
						}

						lblHoten.setText(data.getPatient().getFull_name());
						if (data.getPatient().getGender() == 0) {
							lblGioitinh.setText(getResources().getString(
									R.string.lblNam));
						} else {
							lblGioitinh.setText(getResources().getString(
									R.string.lblNu));
						}
						lblPhonBN.setText(data.getPatient().getPhone());
						lblEmail.setText(data.getPatient().getEmail());

						lblBacsi.setText(data.getDoctor().getFullname());
						if (data.getDoctor().getDegrees() != null
								&& data.getDoctor().getDegrees().size() > 0) {
							lblBangcap.setText(data.getDoctor().getDegrees()
									.get(0));
						}

						if (data.getDoctor().getAssociations() != null
								&& data.getDoctor().getAssociations().size() > 0) {
							lblChuyenmon.setText(data.getDoctor()
									.getAssociations().get(0));
						}

						lblSodienthoai.setText(data.getDoctor().getPhone());

						lblNgaytao.setText(data.getCreatedAt());
						switch (data.getStatus()) {
						case 0:
							lblTrangthai.setText(Html.fromHtml(Utils
									.setTextcolorHtml(
											getResources().getString(
													R.string.cho_duyet),
											"#3883cc")));
							break;

						case -1:
							lblTrangthai.setText(Html.fromHtml(Utils
									.setTextcolorHtml(
											getResources().getString(
													R.string.bac_si_huy),
											"#3883cc")));
							break;
						case -2:
							lblTrangthai.setText(Html.fromHtml(Utils
									.setTextcolorHtml(
											getResources().getString(
													R.string.benh_nhan_huy),
											"#3883cc")));
							break;

						case -3:
							lblTrangthai.setText(Html.fromHtml(Utils
									.setTextcolorHtml(
											getResources().getString(
													R.string.admin_huy),
											"#3883cc")));
							break;
						case -4:
							lblTrangthai.setText(Html.fromHtml(Utils
									.setTextcolorHtml(
											getResources().getString(
													R.string.qua_hen),
											"#3883cc")));
							break;
						default:
							lblTrangthai.setText(Html.fromHtml(Utils
									.setTextcolorHtml(
											getResources().getString(
													R.string.da_duyet),
											"#3883cc")));
							break;
						}

						if (data.getDoctorNotes().length() == 0) {
							/*
							 * lblChuthichBasi.setText(Html.fromHtml(Utils
							 * .setTextcolorHtml( getResources().getString(
							 * R.string.dang_cap_nhat), "#3883cc")));
							 */
							lineBSChuthich.setVisibility(View.GONE);

						} else {
							lineBSChuthich.setVisibility(View.VISIBLE);
							lblChuthichBasi.setText(data.getDoctorNotes());
						}

						if (!data.getPatientNotes().isEmpty()) {
							lblChithichBn.setText(data.getPatientNotes());
							lineBNChuthich.setVisibility(View.VISIBLE);
						} else {
							lineBNChuthich.setVisibility(View.GONE);
						}

					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.GONE);
						Utils.disableEnableControls(true, mView);
					}
				});

	}
}
